/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mx.im.sinocab.dao;

import com.mx.im.sinocab.bean.Employ;
import com.mx.im.sinocab.util.Conection;
import com.mx.im.sinocab.util.Constant;
import com.mx.im.sinocab.util.Conection;
import com.mx.im.sinocab.util.Constant;
import java.util.ArrayList;
//import com.mysql.jdbc.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

 /**
 *
 * @author Administrator
 */
public class DaoEmployConsult {
   
    public PreparedStatement pt;
    
     public Employ consultarEmpleados(int idEmploy) {
     
         Employ unEmpleado = new Employ();
     
         String selectSQL= "SELECT * FROM employ WHERE idEmploy ="+idEmploy+ ";";
        
         try {
             
             Conection cone = new Conection();
             PreparedStatement pt = cone.getConection().prepareStatement(selectSQL);
             ResultSet rs = pt.executeQuery();
            
            
            
            rs.last();
            
                unEmpleado.setIdEmploy(rs.getInt(1));
                unEmpleado.setName(rs.getString(2));
                unEmpleado.setLastName(rs.getString(3));
                unEmpleado.setPhone(rs.getString(4));
                unEmpleado.setCellphone(rs.getString(5));
                unEmpleado.setAddress(rs.getString(6));
                unEmpleado.setGender(rs.getString(7));
                unEmpleado.setBirthday(rs.getDate (8));
                unEmpleado.setAge(rs.getString(9));
                unEmpleado.setEmail(rs.getString(10));
                unEmpleado.setAdmissionDate(rs.getDate(11));
                unEmpleado.setTimeLaboring(rs.getString(12));
                unEmpleado.setDependentEconomic(rs.getString(13));
                unEmpleado.setWorkedTipe(rs.getString(14));
                unEmpleado.setMaritalStatus(rs.getString(15));
                unEmpleado.setStudies(rs.getString(16));
                
            rs.close();
            pt.close();
            cone.closeConection();

        } catch (SQLException e) {
            System.out.println("DaoEmployConsult - consultaEmpleados(): "
                    + e.getMessage());
        }
        return unEmpleado;
        
    }
     
      public List listEmpleados() {
        List listaEmpleados = new ArrayList();
        String selectSQL = "select idEmploy, name, lastName from employ";
        try {
            Conection cone = new Conection();
            PreparedStatement pt = cone.getConection().prepareStatement(selectSQL);
            ResultSet rs = pt.executeQuery(selectSQL);
            
            while (rs.next()) {
                
                Employ unEmpleado = new Employ();
                unEmpleado.setIdEmploy(rs.getInt(1));
                unEmpleado.setName(rs.getString(2));
                unEmpleado.setLastName(rs.getString(3));
                listaEmpleados.add(unEmpleado);
 
            }
            rs.close();
            pt.close();
            cone.closeConection();

        } catch (SQLException e) {
            System.out.println("DAOEmpleado - listEmpleados() : "
                    + e.getMessage());
        }
        return listaEmpleados;
    }
      
       public static boolean modificaEmpleado(Employ bean) {
        boolean AccionOK = false;

        try {
             Conection cone = new Conection();
            PreparedStatement pstmt = cone.getConection().prepareStatement("update employ "
                    + " set name= '"+bean.getName()+"', "
                    + " lastName= '"+bean.getLastName()+"', "
                    + " phone= '"+bean.getPhone()+"', "
                    + " cellphone = '"+bean.getCellphone()+"', "
                    + " address = '"+bean.getAddress()+"', "
                    + " gender = '"+bean.getGender()+"', "
                    + " birthday = '"+bean.getBirthday()+"', "
                    + " age= '"+bean.getAge()+"', "
                    + " maritalStatus= '"+bean.getMaritalStatus()+"', "
                    + " admissionDate='"+bean.getAdmissionDate()+"', "
                    + " timeLaboring= '"+bean.getTimeLaboring()+"', "
                    + " email = '"+bean.getEmail()+"', "
                    + " workedTipe = '"+bean.getWorkedTipe()+"', "
                    + " telefono= '"+bean.getPhone()+"', "
                    + " email= '"+bean.getEmail()+"', "
                    + " dependentEconomic='"+bean.getDependentEconomic()+"', "
                    + " studies= '"+bean.getStudies()+"', "
                    + " where idEmploy = '"+bean.getIdEmploy()+"'");

            AccionOK = pstmt.executeUpdate() == 1;

            pstmt.close();
            cone.closeConection();

        } catch (SQLException e) {
            System.out.println("Actualiza" + e.getMessage());
        }
        return AccionOK;
    }

    public static boolean eliminarEmpleado(Employ bean) {
        
        boolean AccionOK = false;
        

        try {
            Conection cone = new Conection();
            PreparedStatement pt = cone.getConection().prepareStatement("delete from employ "
                    + " where idEmploy = '"+bean.getIdEmploy()+"' ");

            AccionOK = pt.executeUpdate() == 1;

            pt.close();
            cone.closeConection();

        } catch (SQLException e) {
            System.out.println("Elimina):" + e.getMessage());
        }
        return AccionOK;
    }
     
    
    
}
